home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 80x0393.zip / ISR.TXT < prev    next >
Text File  |  1993-03-30  |  4KB  |  112 lines

  1. From: Inbar Raz
  2. Subj: Releasing memory of an ISR that hooks vectors
  3. ________________________________________________________________________
  4.  
  5. Hello everyone.
  6.  
  7. Some  time ago, there was this debate here about the steps needed to  be 
  8. taken  in  order  for a program to remove itself from  memory.  The  big 
  9. obstacle  turned out to be the vector problem - what should the  exiting 
  10. program set the vector to, since it is possible that another program has 
  11. already hooked that vector, and therefore setting the vector to OUR  old 
  12. stored vector could hang the machine.
  13.  
  14. Well,  when I was talking the other day with Yossi Gottlieb, I  suddenly 
  15. came  up  with a GREAT way to solve the problem! You don't  release  ALL 
  16. your  memory  - you leave (number of hooked vectors*5) bytes  in  memory 
  17. ONLY.
  18.  
  19. HOW?  Well,  when  you  set your vector to YOUR ISR,  do  this:  In  the 
  20. beginning of the ISR, put a FAR JMP, that will point to the beginning of 
  21. the  code,  within your segment. When you want to  release  the  memory, 
  22. simply  copy  the ORIGINAL vector into the FAR JMP,  and  therefore  any 
  23. program  that will jump to YOUR ISR address, will automatically jump  to 
  24. the original vector!
  25.  
  26. If  you  didn't quite understand it, or don't know how to  put  it  into 
  27. assembler lines, here is an example I wrote in 3 minutes:
  28.  
  29. ----------------------------- cut here ---------------------------------
  30.  
  31.         .model tiny
  32.         .code
  33.  
  34.         org     00100h
  35.  
  36. Start   jmp     Begin
  37.  
  38. New21   proc    far
  39.  
  40.         EVEN
  41.  
  42.         db      0EAh                    ; Jmp far
  43. My21off dw      offset 21Start
  44. My21seg dw      ?
  45. Old21   dd      ?
  46.  
  47. 21Start:
  48.  
  49. ;       <Your ISR here>
  50. ;
  51. ;       iret / jmp dword ptr cs:[Old21]         ; The usual thing
  52.  
  53. ; now comes the part that erases us from memory
  54.  
  55. QuitTsr:
  56.  
  57. ; First, alter the JMP FAR to point to the ORIGINAL vector
  58.         cli
  59.         mov     ax,word ptr cs:[Old21]
  60.         mov     word ptr cs:[My21off],ax
  61.         mov     ax,word ptr cs:[Old21+2]
  62.         mov     word ptr cs:[My21seg],ax
  63.         sti
  64.  
  65. ; Second, but I'm not QUITE sure this is what we need to do - resize our
  66. ; memory block.
  67.  
  68.         mov     ah,04Ah                 ; Resize allocated block
  69.         push    cs
  70.         pop     es
  71.         mov     bx,(offset Old21 shl 4 ) + 1
  72.         pushf
  73.         call    dword ptr cs:[Old21]
  74.  
  75. ; Third, since we turned ourselves off from within the interrupt, return 
  76. ; to the caller. You might want to alter this to something else.
  77.  
  78.         iret
  79.  
  80. New21   endp
  81.  
  82. Begin:  mov     ax,03521h                       ; Get current vector
  83.         int     021h
  84.  
  85.         mov     word ptr [Old21],bx             ; Store it
  86.         mov     word ptr [Old21+2],es
  87.  
  88.         mov     word ptr [My21seg],cs           ; Complete JMP opcode
  89.  
  90.         mov     ax,02521h                       ; Set new vector for 21h
  91.         lea     dx,New21
  92.         int     021h
  93.  
  94.         lea     dx,Begin                        ; Stay resident
  95.  
  96. end     Start
  97.         int     027h
  98.  
  99. ----------------------------- cut here ---------------------------------
  100.  
  101. If  you are afraid to use the 04Ah, you might choose a safer,  yet  more 
  102. difficult,  way of setting memory: If your MCB is 'Z', turn it  to  'M'. 
  103. Change  the size of your MCB to what it takes to keep the  vectors,  and 
  104. manually create another MCB, this time 'Z', in the following  paragraph, 
  105. and  set YOUR MCB and the new one's sizes, accordingly. If your  MCB  is 
  106. ORIGINALLY  'M', then both the original and the newly created MCBs  will 
  107. be 'M'.
  108.  
  109. Any comments, corrections and ideas are welcomed.
  110.  
  111. Inbar Raz
  112.